import 'package:dio/dio.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_controller.dart';
import 'package:library_base/widget/multi_state_widget/multi_controller.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

abstract class BaseListController<E, I> extends BaseController {
  late EasyRefreshController easyRefreshController;

  MultiController multiControllerOut = MultiController();

  @override
  void onCreate() {
    super.onCreate();
    enableLoadMore = true;
  }

  ///获取控制器
  void setRefreshController(EasyRefreshController easyRefreshController) {
    this.easyRefreshController = easyRefreshController;
  }

  ///是否能下拉刷新
  bool isCanRefresh = true;

  ///是否支持刷新
  bool useRefresh = true;

  ///分页加载 page
  int page = 1;

  ///分页一次加载的数据
  int limit = 50;

  ///是否能上拉加载
  bool enableLoadMore = false;

  ///列表时无数据时是否展示底部footer
  bool isShowNoMoreFooter = false;

  /// 是否首次加载,加载成功后并且数据列表不为0的时候则设置为false
  bool isFirstLoading = true;

  /// 实体类
  RxList<I> listData = <I>[].obs;

  Future requestGeneralListData(
    String url, {
    bool isRefresh = false,
    FBResultFul method = FBResultFul.POST,
    dynamic params,
    Options? options,
    bool isShowErrorToastTip = false,
    Map<String, dynamic>? queryParameters,
  }) {
    requestUniversal<E>(url,
        isSendShowDialog: false,
        isShowErrorToastTip: false,
        params: params,
        options: options,
        queryParameters: queryParameters,
        method: method, resultSuccessCall: (data) {
      if(isFirstLoading || isRefresh){
        page = 1;
      }
      page++;
      if (isFirstLoading) {
        listData.value = transformObject(data, isRefresh);
        loadAfter();
        if (listData.value.isNotEmpty) {
          isFirstLoading = false;
          multiControllerOut.viewStateChange(MultiStateStatus.content);
          baseState?.setStateCallBack?.call();
        } else {
          multiControllerOut.viewStateChange(MultiStateStatus.empty);
          baseState?.setStateCallBack?.call();
        }
      } else {
        if (isRefresh) {
          listData.value = transformObject(data, isRefresh);
          loadAfter();
          if(listData.value.isEmpty){
            multiControllerOut.viewStateChange(MultiStateStatus.empty);
          }
          easyRefreshController.finishRefresh();
          easyRefreshController.resetFooter();
        } else {
          List<I> pageList = transformObject(data, isRefresh);
          listData.value.addAll(pageList);
          loadAfter();
          if (isLoadEnd(pageList)) {
            easyRefreshController.finishLoad(IndicatorResult.noMore);
          } else {
            easyRefreshController.finishLoad(IndicatorResult.success);
            baseState?.setStateCallBack?.call();
          }
        }
        baseState?.setStateCallBack?.call();
      }
    }, resultSuccessListCall: (dataList) {
      page++;
      if (isFirstLoading) {
        listData.value = transformList(dataList, isRefresh);
        loadAfter();
        if (listData.value.isNotEmpty) {
          isFirstLoading = false;
          multiControllerOut.viewStateChange(MultiStateStatus.content);
          baseState?.setStateCallBack?.call();
        } else {
          multiControllerOut.viewStateChange(MultiStateStatus.empty);
          baseState?.setStateCallBack?.call();
        }
      } else {
        if (isRefresh) {
          listData.value = transformList(dataList, isRefresh);
          loadAfter();
          easyRefreshController.finishRefresh();
          easyRefreshController.resetFooter();
        } else {
          List<I> pageList = transformList(dataList, isRefresh);
          listData.value.addAll(pageList);
          loadAfter();
          if (isLoadEnd(pageList)) {
            easyRefreshController.finishLoad(IndicatorResult.noMore);
          } else {
            easyRefreshController.finishLoad(IndicatorResult.success);
            baseState?.setStateCallBack?.call();
          }
        }
        baseState?.setStateCallBack?.call();
      }
    }, resultFailCall: (msg, code) {
      if (isFirstLoading) {
        multiControllerOut.viewStateChange(MultiStateStatus.error);
      } else {
        if (isRefresh) {
          easyRefreshController.finishRefresh(IndicatorResult.fail);
          easyRefreshController.resetFooter();
        } else {
          easyRefreshController.finishLoad(IndicatorResult.fail);
        }
      }
      baseState?.setStateCallBack?.call();
    });
    return Future.value();
  }

  void loadAfter(){}

  ///获取应该传进去的页数
  int getPage(bool isRefresh) {
    if (isFirstLoading) {
      return 1;
    } else if (isRefresh) {
      return 1;
    } else {
      return page;
    }
  }

  ///数据转换
  List<I> transformObject(E data, bool isRefresh) => [];

  ///数据转换,默认E类型==I类型则直接返回这个集合
  List<I> transformList(List<E> dataList, bool isRefresh) {
    if (E == I) {
      return dataList as List<I>;
    }
    return [];
  }

  ///是否加载完毕
  bool isLoadEnd(List data) {
    return data.length < limit;
  }

  @override
  void dispose() {
    super.dispose();
    easyRefreshController.dispose();
    // multiControllerOut.dispose();
  }

}
